import { initAdminPage, initGlobal, initUserInfo, initUserState } from './initData'
import {
  ADMIN_ADD_DEPARTMENT,
  ADMIN_ADD_SALARY,
  ADMIN_DELETE_DEPARTMENT,
  ADMIN_DELETE_SALARY,
  ADMIN_DELETE_USER,
  ADMIN_LOAD_ACCESS_TOKEN,
  ADMIN_LOGIN,
  ADMIN_LOGOUT,
  ADMIN_SET_ALL_DEPARTMENT_LIST,
  ADMIN_SET_DEPARTMENT_INFO,
  ADMIN_SET_DEPARTMENT_LIST,
  ADMIN_SET_CURRENT_DEPARTMENT,
  ADMIN_SET_USER_INFO,
  ADMIN_SET_USER_LIST,
  ADMIN_SET_USER_SALARY_LIST,
  ADMIN_UPDATE_DEPARTMENT_INFO,
  ADMIN_UPDATE_SALARY,
  DELETE_USER_STATE,
  LOAD_USER_STATE,
  REGISTER_SUCCESS,
  REMEMBER_USER_STATE,
  REMOVE_USER_STATE,
  SAVE_USER_STATE,
  SET_LOGIN_TYPE,
  SET_SALARY_LIST,
  SET_USER_INFO
} from './action-types'
import {
  delete_user_state,
  get_admin_state,
  get_user_state,
  remember_user_state,
  remove_admin_state,
  remove_user_state,
  save_user_state,
  set_admin_state
} from '../global'

/**
 * 保存用户登陆状态 reducers
 * @param state
 * @param {{type: string, data: { accessToken: string, userId: number }}} action
 * @returns {{accessToken: string, userId: number}}
 */
export function $UserState (state = initUserState, action) {
  switch (action.type) {
    /**
     * 记住登陆状态
     */
    case REMEMBER_USER_STATE:
      remember_user_state(action.data)
      return action.data
    /**
     * 注销登陆，删除登陆状态
     */
    case REMOVE_USER_STATE:
      remove_user_state()
      return initUserState
    /**
     * 保存登陆状态的session storage
     */
    case SAVE_USER_STATE:
      save_user_state(action.data)
      return action.data
    /**
     * 删除保存在session storage 的用户登录状态
     */
    case DELETE_USER_STATE:
      delete_user_state()
      return initUserState
    /**
     * 从localstorage 中加载用户登录状态信息
     */
    case LOAD_USER_STATE:
      return get_user_state()
    default:
      return state
  }
}

/**
 * 用户状态 reducers
 * @param state
 * @param data
 * @param type
 * @returns {{userInfo: {gender: string, phone: string, registerTime: string, name: string, department: string, email: string, age: string, username: string}, salaryList: *[]}|{userInfo: {gender: string, phone: string, registerTime: string, name: string, department: string, email: string, age: string, username: string}, salaryList: *}}
 */
export function $UserInfoState (state = initUserInfo, { data, type }) {
  switch (type) {
    /**
     * 用户信息
     */
    case SET_USER_INFO:
      return { ...state, userInfo: { ...state.userInfo, ...data } }
    /**
     * 用户薪资列表
     */
    case SET_SALARY_LIST: {
      let list = data.map(item => {
        let { postSalary, performanceSalary, workYearSalary, allowanceSalary } = item
        let total = postSalary + performanceSalary + workYearSalary + allowanceSalary
        return ({
          ...item,
          key: item.salaryId,
          total
        })
      })
      return { ...state, salaryList: list }
    }
    default:
      return state
  }
}

/**
 * 管理员页面 数据状态
 * @param state
 * @param type
 * @param data
 * @returns {{userList: *[], salaryList: *[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *[]}|{userList: *[], salaryList: *[], currentDepartment: *, allDepartmentList: *[], departmentList: *[]}|{}|{userList: *[], salaryList: *[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *, departmentList: *[]}|{userList: *[], salaryList: {total: *, key: *}[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *[]}|{userList: *[], salaryList: *[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *[], accessToken: *}|{userList: ({password: *, gender: *, phone: *, departmentId: *, name: *, userId: *, email: *, age: *, username: *}|*)[], salaryList: *[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *[]}|{userList: *, salaryList: *[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *[]}|{userList: *[], salaryList: *[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *}|{userList: *[], salaryList: (*)[], currentDepartment: {phone: string, name: string, place: string, time: string, parentId: string}, allDepartmentList: *[], departmentList: *[]}}
 */
export function $AdminPageState (state = initAdminPage, { type, data }) {
  switch (type) {
    /**
     * 管理员登陆
     */
    case ADMIN_LOGIN:
      set_admin_state(data)
      return { ...state, ...data }
    /**
     * 管理员注销
     */
    case ADMIN_LOGOUT:
      remove_admin_state()
      return {}
    /**
     * 用户列表
     */
    case ADMIN_SET_USER_LIST: {
      let list = data.map(item => ({
        ...item,
        key: item.userId,
      }))
      return { ...state, userList: list }
    }
    /**
     * 删除用户
     */
    case ADMIN_DELETE_USER: {
      let { userList } = state
      let list = [...userList]
      list = list.filter(({ userId }) => userId !== data)
      return { ...state, userList: list }
    }
    /**
     * 删除薪资
     */
    case ADMIN_DELETE_SALARY: {
      let { salaryList } = state
      let list = [...salaryList]
      list = list.filter(item => item.salaryId !== data)
      return { ...state, salaryList: list }
    }
    /**
     * 更新薪资
     */
    case ADMIN_UPDATE_SALARY: {
      debugger
      let { salaryList } = state
      let list = salaryList.map(item => {
        return item.salaryId !== data.salaryId ? item : data
      })
      return { ...state, salaryList: list }
    }
    /**
     * 添加薪资
     */
    case ADMIN_ADD_SALARY: {
      let { salaryList } = state
      let list = [...salaryList]
      list.push(data)
      return { ...state, salaryList: list }
    }
    /**
     * 设置用户薪资列表
     */
    case ADMIN_SET_USER_SALARY_LIST: {
      let list = [...data]
      list = list.map(item => {
        let { postSalary, performanceSalary, workYearSalary, allowanceSalary } = item
        let total = postSalary + performanceSalary + workYearSalary + allowanceSalary
        return {
          ...item,
          total,
          key: item.salaryId
        }
      })
      return { ...state, salaryList: list }
    }
    /**
     * 设置部门信息
     */
    case ADMIN_SET_DEPARTMENT_INFO:
      return { ...state }
    /**
     * 更新部门信息
     */
    case ADMIN_UPDATE_DEPARTMENT_INFO:
      return { ...state, currentDepartment: data }
    /**
     * 删除部门
     */
    case ADMIN_DELETE_DEPARTMENT: {
      let id = data
      let { departmentList } = state
      let list = departmentList.filter(item => item.id !== id)
      return { ...state, departmentList: list }
    }
    /**
     * 设置部门列表
     */
    case ADMIN_SET_DEPARTMENT_LIST: {
      let list = data.map(item => ({ ...item, key: item.id }))
      return { ...state, departmentList: list }
    }
    /**
     * 所有部门列表
     */
    case ADMIN_SET_ALL_DEPARTMENT_LIST: {
      let list = data.map(item => ({ ...item, key: item.id }))
      return { ...state, allDepartmentList: list }
    }
    /**
     * 加载管理员 AccessToken
     */
    case ADMIN_LOAD_ACCESS_TOKEN:
      let accessToken = get_admin_state()
      return { ...state, accessToken }
    /**
     * 设置用户信息
     */
    case ADMIN_SET_USER_INFO: {
      let { userId, departmentId, password, username, email, name, age, gender, phone } = data
      let { userList } = state
      let list = userList.map(item => {
        if (item.userId === userId) {
          return {
            userId, departmentId, password, username, email, name, age, gender, phone
          }
        } else return item
      })
      return { ...state, userList: list }
    }
    /**
     * 添加部门
     */
    case ADMIN_ADD_DEPARTMENT: {
      let { departmentList } = state
      let list = [...departmentList]
      list.push(data)
      return { ...state, departmentList: list }
    }
    /**
     * 当前部门信息
     */
    case ADMIN_SET_CURRENT_DEPARTMENT: {
      return { ...state, currentDepartment: data }
    }
    default:
      return state
  }
}

/**
 *全局数据
 * @param state
 * @param data
 * @param type
 * @returns {{loginType: 'admin' | 'user' }|{}|{registerSuccess: *}}
 */
export function $GlobalState (state = initGlobal, { data, type }) {
  switch (type) {
    /**
     * 注册状态
     */
    case REGISTER_SUCCESS:
      return { ...state, registerSuccess: data }
    /**
     * 登陆的用户类型
     */
    case SET_LOGIN_TYPE:
      return { ...state, loginType: { admin: 'admin', user: 'user' }[data] } // 1 为 普通用户， 2 为管理员
    default:
      return state
  }
}